﻿using System;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
using System.Text;

namespace ScottLane.MediaFeeder.WebService
{
    /// <summary>
    /// Contains helper methods used throughout the service.
    /// </summary>
    public class Utility
    {
        /// <summary>
        /// Base64 encodes the input string.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <returns>The Base64 encoded string.</returns>
        public static string Base64Encode(string input)
        {
            return Convert.ToBase64String(Encoding.UTF8.GetBytes(input));
        }

        /// <summary>
        /// Base64 decodes the input string.
        /// </summary>
        /// <param name="input">The encoded input string.</param>
        /// <returns>The decoded string.</returns>
        public static string Base64Decode(string input)
        {
            Decoder decoder = Encoding.UTF8.GetDecoder();
            byte[] bytes = Convert.FromBase64String(input);
            int characterCount = decoder.GetCharCount(bytes, 0, bytes.Length);
            char[] characters = new char[characterCount];
            decoder.GetChars(bytes, 0, bytes.Length, characters, 0);
            return new String(characters);
        }

        /// <summary>
        /// Gets the appropriate feed renderer based on the query string.
        /// </summary>
        /// <param name="feed">The feed.</param>
        /// <returns>The feed formatter.</returns>
        public static SyndicationFeedFormatter GetFormatter(SyndicationFeed feed)
        {
            string query = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["format"];
            SyndicationFeedFormatter formatter = null;

            if (query == "atom")
            {
                formatter = new Atom10FeedFormatter(feed);
            }
            else
            {
                formatter = new Rss20FeedFormatter(feed);
            }

            return formatter;
        }

        private static string _baseUrl;

        /// <summary>
        /// Gets the base url of the WCF endpoint.
        /// </summary>
        public static string BaseUrl
        {
            get
            {
                if (_baseUrl == null)
                {
                    Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);
                    string address = serviceModel.Services.Services[0].Endpoints[0].Address.ToString();
                    string requestUrl = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.AbsoluteUri;
                    _baseUrl = requestUrl.Substring(0, requestUrl.IndexOf(address) + address.Length);
                }

                return _baseUrl;
            }
        }
    }
}
